<?php

namespace app\admin\service;

use app\admin\model\Link as LinkModel;

class LinkService
{
    protected $model;
    protected $categoryService;

    public function __construct(){
        $this->model = app()->make(LinkModel::class);
    }

    /**
     * @param $where
     * @param $LinkSize
     * @return LinkModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function queryListByLink($where, $LinkSize = 15)
    {
        $list = $this->model->field('id, name, type, target, url, created_time')->order('created_time desc')->paginate($LinkSize);
        return $list;
    }

    /**
     * @param int $id
     * @return LinkModel|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function queryDetail(int $id){
        $detail = $this->model->where('id', $id)->find();
        return $detail;
    }
    public function save(array $data): int
    {
        //print_r($data);
        $result = null;
        if (empty($data['id'])) {
            $this->model->insert($data);
            return $this->model->getLastInsID();
        } else {
            $this->model->where('id', $data['id'])->save($data);
            return $data['id'];
        }
    }

    public function delete(int $id): bool
    {
        return $this->model->where('id', $id)->delete();
    }
}